home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Commun⁄Network
/
seer_family.v2.0
/
Timing
/
timer.Pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1987-08-29
|
5KB
|
216 lines
PROGRAM TestTimer;
{Test millisecond timing routines. }
USES
TimingRoutines;
CONST
MaxTime = 3000;
MinTime = 100;
AppleMenu = 1;
FileMenu = 2;
EditMenu = 3;
VAR
count : LongInt; {Milliseconds}
state : (waiting, starting, running, done);
Event : EventRecord;
TextRect, DrawingRect : Rect;
trial, sum : LongInt;
PROCEDURE init;
BEGIN
InitCursor;
state := waiting;
SetRect(TextRect, 25, 50, 325, 320);
SetTextRect(TextRect);
SetRect(DrawingRect, 350, 50, 475, 175);
SetDrawingRect(DrawingRect);
SetRect(DrawingRect, 0, 0, 125, 125);
ShowText;
ShowDrawing;
TextSize(12);
TextFont(0);
Writeln('Click the mouse button when you ');
Writeln('see GO in the Drawing window.');
Writeln;
Writeln('Select START from FILE Menu to begin...');
Writeln;
InitTimer;
sum := 0;
trial := 1;
END;
PROCEDURE InitMenus;
{ Init applications menus, add desk accessories to apple menu. }
VAR
MenuH : MenuHandle;
Apple : Str255;
BEGIN
Apple := chr(20);
Menuh := NewMenu(AppleMenu, Apple);
AddResMenu(MenuH, 'DRVR');
IF (CountMItems(MenuH) = 0) THEN
AppendMenu(MenuH, '(No accessories');
InsertMenu(MenuH, 0);
MenuH := NewMenu(FileMenu, 'File');
AppendMenu(MenuH, 'Start/S;Quit/Q');
InsertMenu(MenuH, 0);
MenuH := NewMenu(EditMenu, 'Edit');
AppendMenu(MenuH, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
InsertMenu(MenuH, 0);
DrawMenuBar;
END; { initmenus }
PROCEDURE DoMenu (VAR Event : EventRecord;
MenuChoice : LongInt);
{ handle mouse click in menu bar; true if user chooses quit. }
VAR
MenuID, MenuItem : integer;
ItemName : str255;
Junk : integer;
SavePort : GrafPtr;
BEGIN
MenuID := HiWord(MenuChoice);
MenuItem := LoWord(MenuChoice);
CASE MenuID OF
AppleMenu :
BEGIN
GetItem(GetMHandle(AppleMenu), MenuItem, ItemName);
GetPort(SavePort);
Junk := OpenDeskAcc(ItemName);
SetPort(SavePort);
END;
FileMenu :
CASE MenuItem OF
1 :
state := starting;
2 :
state := done;
END;
EditMenu :
BEGIN
GetItem(GetMHandle(EditMenu), MenuItem, ItemName);
IF NOT SystemEdit(MenuItem - 1) THEN
; { application handles, do nothing for now. }
END;
OTHERWISE
; { No Selection was made }
END;
HiLiteMenu(0);
END; { DoMenu }
PROCEDURE DoMouse (VAR Event : EventRecord);
{ handle mouse-down events; true if user chooses to quit. }
VAR
ThePart : integer;
WhichWindow : WindowPtr;
BEGIN
ThePart := FindWindow(Event.Where, WhichWindow);
CASE ThePart OF
InMenuBar :
DoMenu(Event, MenuSelect(Event.Where));
InSysWindow :
SystemClick(Event, WhichWindow);
InDesk, InContent, InDrag, InGrow, InGoAway :
;
OTHERWISE
;
END;
END; { DoMouse }
PROCEDURE DoKey (VAR Event : EventRecord);
{ handle key-down events. }
VAR
c : char;
BEGIN
c := chr(BitAnd(Event.Message, 127));
IF (BitAnd(Event.Modifiers, CmdKey) = CmdKey) THEN
DoMenu(Event, MenuKey(c));
END; { DoKey }
FUNCTION ButtonDown : boolean;
{ Returns true if mouse button is down; reads VIA directly because}
{mousedown events are posted only at vertical retrace. }
CONST
VIABase = $1D4;
TYPE
VIA = PACKED ARRAY[0..7680] OF byte;
VAR
VIAPtr : ^VIA;
BasePtr : ^LongInt;
BEGIN
BasePtr := Pointer(VIABase);
VIAptr := Pointer(BasePtr^);
ButtonDown := (BitAnd(ViaPtr^[0], 8) = 0);
END;
PROCEDURE HandlerEvents;
BEGIN
SystemTask;
IF GetNextEvent(EveryEvent, Event) THEN
CASE Event.What OF
MouseDown :
DoMouse(Event);
KeyDown :
DoKey(Event);
OTHERWISE
; { ignore all other events }
END;
END;
PROCEDURE DoOneTrial;
LABEL
99;
VAR
ticks, SaveTicks : LongInt;
BEGIN
SaveTicks := TickCount;
REPEAT
HandlerEvents;
IF state = done THEN
GOTO 99;
UNTIL TickCount >= (SaveTicks + Random MOD 120 + (120));
MoveTo(50, 50);
REPEAT
UNTIL (TickCount <> ticks); {Wait for vertical retrace}
DrawString('Go');
ResetTimer;
REPEAT
count := TimerValue;
UNTIL ButtonDown OR (count >= MaxTime);
MoveTo(50, 50);
EraseRect(DrawingRect);
IF (Count < MaxTime) THEN
BEGIN
sum := sum + count;
writeln(trial : 10, Count : 10, sum / trial : 10 : 1);
trial := trial + 1;
END
ELSE
writeln;
99 :
END;
BEGIN { main }
FlushEvents(EveryEvent, 0);
InitMenus;
init;
REPEAT
IF state = starting THEN
BEGIN
writeln('Get Ready!');
writeln;
writeln('Trial' : 12, 'Time' : 9, 'Mean' : 8);
state := running;
END;
IF state = running THEN
DoOneTrial;
HandlerEvents;
UNTIL state = Done;
KillTimer;
END.